Sequential verification when editing model

Description

With the Next Design extension, you can implement the function to receive model operations and file operations in Next Design as events and add unique processing to each event.

In the following, based on the extension created in the previous tutorial: Batch model verification, let's expand the function to sequentially verify the model according to the model edit operation.

  • Overall flow

    • Manifest event extension point definition
    • Call of verification processing by event handler

Goal image

Screen capture or GIF animation

  • Open the [Advanced Driving System Software Development] project from the Next Design sample project.

  • If you select one of the models, rename it and confirm, the model will be validated according to the following validation rules, and any errors will be displayed.

    Validation rule: The model name must not contain single-byte space characters.

download

  • You can download the complete set of files created as a result of this tutorial from the following link.

    Download link: ValidationSample-2.zip

Event extension point definition by manifest

The name of the model is one of the fields that the model has. The name change is received as a model field change event.

To receive the model's field change event and call the event handler, define the following in your manifest:

  • Event extension point definition

json { "extensionPoints": { "events": { //Event extension point definition } } }

Implementation code

manifest.json

{
  //extension definition
  "name": "Validation Sample",
  "version": "1.1.0",
  "publisher": "DENSO CREATE INC.",
  "license": "Next Design License Agreement. Copyright (C) 2019 DENSO CREATE INC. All rights reserved.",

  "main": "ValidationSample.dll", //Specify the build result DLL file name as the entry point.
  "lifecycle": "project", //Specify the project lifecycle as the lifecycle.
  "baseprofile": "In-vehicle system software development", //Specify the profile name as the condition of the target project.

  //extension point definition
  "extensionPoints": {
    //ribbon
    "ribbon": {
      "tabs": [
        //Define a ribbon tab to add for the extension.
        {
          "id": "MyExtensions.MainTab",
          "label": "My Extensions",
          "orderAfter": "System.Help",
          "groups": [
            //Define a group that separates the ribbon tabs.
            {
              "id": "MyExtensions.Validation.Group",
              "label": "Model validation",
              "controls": [
                //Define a verification execution button.
                {
                  "type": "Button",
                  "id": "MyExtensions.Validation.RunButton",
                  "label": "verification",
                  "description": "Verify all models",
                  "imageLarge": "resources/icon.png",
                  "command": "Command.Validation.Run" //Specify the id of the validation command defined in the command below.
                }
              ]
            }
          ]
        }
      ]
    },

    //command
    "commands": [
      //Define a validation command that calls the validation handler `Run`.
      {
        "id": "Command.Validation.Run",
        "execFunc": "Run", //Specifies the public method implemented in the entry point main class.
        "canExecWhen": {
          "uiState": "ProjectOpened" //Specifies that the project is open as a valid condition for the command.
        }
      }
    ],

    //event
    "events": {
      //Add an extension point for events related to the model.
      "models": [
        {
          "class": "*", //Specify "*" in the event filter or omit this line when performing verification processing common to all models.
          //If you want to perform verification processing only for a specific class, list the target class names in the event filter separated by commas.
          //"class": "TechnicalArchitectureComponentECU,TechnicalArchitectureComponentSensor",
          "onFieldChanged": "OnFieldChanged" //Register the public method implemented in the entry point main class as an event handler when the field of the model changes.
        }
      ]
    }
  }
}

Calling name verification process by event handler

In the event handler called from the event, to check the details of the event and call the name verification process, implement the following contents as a public method.

  • Model field change event handler

cs public class ValidationSample :IExtension { //model field change event handler public void OnFieldChanged(IEventContext context, IEventParams eventParams) { //Handle event handler } }

Implementation code

ValidationSample.cs

//Declaration of the namespace of the API referenced in extension development
using NextDesign.Extension;
using NextDesign.Core;
using NextDesign.Desktop;

///<summary>
///Main class of the entry point DLL
///</summary>
///<remarks>
///Implement the `IExtension` interface in the main class of the entry point.
///Implement the handler as a public method of this class.
///</remarks>
public class ValidationSample :IExtension
{
    ///<summary>
    ///Command handler for verification processing
    ///</summary>
    ///<param name="context">command context</param>
    ///<param name="parameters">command parameters</param>
    ///<remarks>
    ///Implement the verification process command handler as a public method of the main class.
    ///</remarks>
    public void Run(ICommandContext context, ICommandParams parameters)
    {
        var app = context.App;
        var project = app.Workspace.CurrentProject;

        //Clear all previous errors.
        app.Errors.ClearErrors();

        //Display the error list window.
        app.Window.IsInformationPaneVisible = true;
        app.Window.ActiveInfoWindow = "Error";

        //Iterate over all models in the project.
        var models = project.GetAllChildren();
        foreach (var model in models)
        {
            //Validate the model according to the validation rules.
            ValidateModel(model);
        }
    }

    ///<summary>
    ///Model field change event handler
    ///</summary>
    ///<param name="context">event context</param>
    ///<param name="eventParams">Event parameters</param>
    ///<remarks>
    ///Implement the field change event handler of the model as a public method of the main class.
    ///</remarks>
    public void OnFieldChanged(IEventContext context, IEventParams eventParams)
    {
        //Detailed information such as the field changed model and field name is obtained from the event parameter passed to the second argument of the event handler.
        //Since the concrete type of the event parameter differs depending on the type of event, convert it to the event parameter for the field change event of the model.
        var fieldChangeParams = eventParams as ModelFieldChangedEventParams;
        var model = fieldChangeParams.Model; //field changed model
        var fieldName = fieldChangeParams.Field; //field name of field changed
        var app = context.App;

        //Only validate if the changed field is the `Name` field.
        if (fieldName == "Name")
        {
            //Clear the previous error for the target model.
            app.Errors.ClearErrorsAt(model);

            //Validate the model according to the validation rules.
            ValidateModel(model);
        }
    }

    ///<summary>
    ///Validate model according to validation rules
    ///</summary>
    ///<param name="model">model</param>
    private void ValidateModel(IModel model)
    {
        //Match the following validation rules.
        //・Do not include spaces in the model name
        if (model.Name.IndexOf(" ")> 0)
        {
            //If it does not meet the validation rules, add error information to the corresponding model.
            var message = string.Format("The model name contains spaces. Model name: {0}", model.Name);
            var error = model.AddError("Name", "Error", "Model naming convention check", message);
        }
    }

    ///<summary>
    ///Extension initialization
    ///</summary>
    ///<param name="context">Execution context</param>
    ///<remarks>
    ///Even if you do not need extension initialization/termination processing, empty `Activate` and `Deactivate` methods are required.
    ///</remarks>
    public void Activate(IContext context)
    {
        //Implement extension initialization etc. if necessary.
    }

    ///<summary>
    ///Extension end processing
    ///</summary>
    ///<param name="context">Execution context</param>
    ///<remarks>
    ///Even if you do not need extension initialization/termination processing, empty `Activate` and `Deactivate` methods are required.
    ///</remarks>
    public void Deactivate(IContext context)
    {
        //Implement extension termination processing etc. if necessary.
    }
}